package com.ivy.wechatforclient.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ivy.wechatforclient.R;
import com.ivy.wechatforclient.fragment.ContactFragment;
import com.ivy.wechatforclient.fragment.FindFragment;
import com.ivy.wechatforclient.fragment.MeFragment;
import com.ivy.wechatforclient.fragment.WeixinFragment;
import com.ivy.wechatforclient.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

/**
 * Created by Lenovo on 2017/11/17.
 */

public class InitActivity extends AppCompatActivity implements View.OnClickListener {
   // private ViewPager viewPager;// 声明一个viewpager对象

    private TextView tv_title;
    private ImageView iv_search;
    private ImageView iv_add;

    private WeixinFragment weixinFragment;
    private ContactFragment contactFragment;
    private FindFragment findFragment;
    private MeFragment meFragment;

    private ImageButton ib_weixin;
    private ImageButton ib_contact;
    private ImageButton ib_find;
    private ImageButton ib_me;

    private TextView tv_weixin;
    private TextView tv_contect;
    private TextView tv_find;
    private TextView tv_me;

    private String owner_tel;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
        init();
    }

    @Override
    public void onClick(View v) {
        //  先清除图标
        clearIcon();
        FragmentManager manager = getFragmentManager();
        final FragmentTransaction transaction = manager.beginTransaction();
        switch (v.getId()){
            case R.id.ib_weixin:
                if(weixinFragment == null){
                    weixinFragment = new WeixinFragment();
                }
                transaction.replace(R.id.fragment_container,weixinFragment);
                ib_weixin.setImageResource(R.drawable.weixin_pressed);
                tv_weixin.setTextColor(Color.rgb(0,208,13));
                break;
            case R.id.ib_contact:
                if(contactFragment == null){
                    contactFragment = new ContactFragment(getApplicationContext());
                }
                Bundle bundle = new Bundle();
                bundle.putString("owner_tel", owner_tel);
                contactFragment.setArguments(bundle);

                transaction.replace(R.id.fragment_container,contactFragment);
                ib_contact.setImageResource(R.drawable.contact_list_pressed);
                tv_contect.setTextColor(Color.rgb(0,208,13));

                break;
            case R.id.ib_find:
                if(findFragment == null){
                    findFragment = new FindFragment();
                }
                transaction.replace(R.id.fragment_container,findFragment);
                ib_find.setImageResource(R.drawable.find_pressed);
                tv_find.setTextColor(Color.rgb(0,208,13));
                break;
            case R.id.ib_me:
                if(meFragment == null){
                    meFragment = new MeFragment();
                }
                transaction.replace(R.id.fragment_container,meFragment);
                ib_me.setImageResource(R.drawable.profile_pressed);
                tv_me.setTextColor(Color.rgb(0,208,13));
                break;
        }
        transaction.commit();
    }

    private void init() {
        Intent intent = getIntent();
        owner_tel = intent.getStringExtra("owner_tel");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_search = (ImageView) findViewById(R.id.iv_search);
        iv_add = (ImageView) findViewById(R.id.iv_add);

        ib_weixin = (ImageButton) findViewById(R.id.ib_weixin);
        ib_contact = (ImageButton) findViewById(R.id.ib_contact);
        ib_find = (ImageButton) findViewById(R.id.ib_find);
        ib_me = (ImageButton) findViewById(R.id.ib_me);

        tv_weixin = (TextView) findViewById(R.id.tv_weixin);
        tv_contect = (TextView) findViewById(R.id.tv_contact);
        tv_find = (TextView) findViewById(R.id.tv_find);
        tv_me = (TextView) findViewById(R.id.tv_me);

        ib_weixin.setOnClickListener( this);
        ib_contact.setOnClickListener( this);
        ib_find.setOnClickListener( this);
        ib_me.setOnClickListener(this);

        //  启动点击页面
        ib_weixin.performClick();
    }

    private void clearIcon() {
        ib_weixin.setImageResource(R.drawable.weixin_normal);
        ib_contact.setImageResource(R.drawable.contact_list_normal);
        ib_find.setImageResource(R.drawable.find_normal);
        ib_me.setImageResource(R.drawable.profile_normal);

        tv_weixin.setTextColor(Color.rgb(154,154,154));
        tv_contect.setTextColor(Color.rgb(154,154,154));
        tv_find.setTextColor(Color.rgb(154,154,154));
        tv_me.setTextColor(Color.rgb(154,154,154));
    }


}
